package com.cloud.kafka;

import org.apache.kafka.clients.consumer.ConsumerRecord;
import org.apache.kafka.clients.consumer.ConsumerRecords;
import org.springframework.kafka.annotation.KafkaListener;
import org.springframework.kafka.support.Acknowledgment;
import org.springframework.stereotype.Component;

@Component
public class KafkaConsumer {

    //批量消费,ConsumerRecords<?,?>用于批量消费消息
    @KafkaListener(topics = "testTopichty113")
    public void onMessageshkf(ConsumerRecords<?, ?> records, Acknowledgment acknowledgment) {
        System.out.println("消费数量=" + records.count());
        for (ConsumerRecord<?, ?> record : records) {

            //消费的哪个topic、partition(哪个分区)的消息，打印出消息内容
            System.out.println("testTopichty113消费：" + record.topic() + "-" + record.partition() + "-" + record.key() + "-" + record.value());
//            List<MallCustAccount> custAccounts = JSON.parseArray(record.value().toString(), MallCustAccount.class);
//            System.out.println(custAccounts);
        }
//        acknowledgment.acknowledge();

    }

    //    @KafkaListener(topics = "RepairTest")
    public void RepairTest(ConsumerRecords<?, ?> records, Acknowledgment acknowledgment) {
        System.out.println("消费数量=" + records.count());
        for (ConsumerRecord<?, ?> record : records) {

            //消费的哪个topic、partition(哪个分区)的消息，打印出消息内容
            System.out.println("RepairTest：" + record.topic() + "-" + record.partition() + "-" + record.key() + "-" + record.value());
//            List<MallCustAccount> custAccounts = JSON.parseArray(record.value().toString(), MallCustAccount.class);
//            System.out.println(custAccounts);
        }
//        acknowledgment.acknowledge();

    }

    @KafkaListener(topics = {"RepairTest"}, groupId = "test-group2")
    public void listener(ConsumerRecord<String, String> record) {
        //获取消息
        String message = record.value();
        //消息偏移量
        long offset = record.offset();
        System.out.println("testTopic-test-group2：" + message + "\n当前偏移量：" + offset);
    }

}
